﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using QuanLyDaiLy.Model;
using QuanLyDaiLy.Platform;
using QuanLyDaiLy.Context;

namespace QuanLyDaiLy.View
{
    /// <summary>
    /// Interaction logic for WMatHang.xaml
    /// </summary>
    public partial class WMatHang : Page
    {
        private CMatHang mathang;
        private CMatHangContext mathangContext;

        #region Function

        public WMatHang()
        {
            InitializeComponent();
            InitializeFeature();
        }

          private void InitializeFeature()
        {
            mathangContext = new CMatHangContext(CConstant.CONNECTION);
            try
            {
                TaoBangDuLieu();
                TrangThaiMacDinh();
                CapNhatDuLieuBang();
            }
            catch { }
        }

        private void TaoBangDuLieu()
        {
            try
            {
                GridView grdView = new GridView();
                grdView.AllowsColumnReorder = true;
                grdView.ColumnHeaderToolTip = "MATHANG";

                GridViewColumn mamathang = new GridViewColumn();
                mamathang.DisplayMemberBinding = new Binding("MaMatHang");
                mamathang.Header = "Mã Mặt Hàng";
                mamathang.Width = 120;
                grdView.Columns.Add(mamathang);

                GridViewColumn tenmathang = new GridViewColumn();
                tenmathang.DisplayMemberBinding = new Binding("TenMatHang");
                tenmathang.Header = "Tên Mặt Hàng";
                tenmathang.Width = 120;
                grdView.Columns.Add(tenmathang);

                GridViewColumn soluongton = new GridViewColumn();
                soluongton.DisplayMemberBinding = new Binding("SoLuongTon");
                soluongton.Header = "Số Lượng Tồn";
                soluongton.Width = 120;
                grdView.Columns.Add(soluongton);

                GridViewColumn dongiaquydinh = new GridViewColumn();
                dongiaquydinh.DisplayMemberBinding = new Binding("DonGiaQuyDinh");
                dongiaquydinh.Header = "Đơn Giá Quy Định";
                dongiaquydinh.Width = 120;
                grdView.Columns.Add(dongiaquydinh);

               
                lvMatHang.View = grdView;
            }
            catch { }
        }

        private void CapNhatDuLieuBang()
        {
            lvMatHang.ItemsSource = mathangContext.LayDanhSachTatCa();
        }

        private void ChonMatHang()
        {
            try
            {
                TrangThaiCapNhat();
                if (lvMatHang.SelectedItem is CMatHang)
                {
                    txtMaMatHang.Text = (lvMatHang.SelectedItem as CMatHang).MaMatHang.ToString();
                    txtTenMatHang.Text = (lvMatHang.SelectedItem as CMatHang).TenMatHang.ToString();
                    txtSoLuongTon.Text = (lvMatHang.SelectedItem as CMatHang).SoLuongTon.ToString();
                    txtDonGiaQuyDinh.Text = (lvMatHang.SelectedItem as CMatHang).DonGiaQuyDinh.ToString();
                }
            }
            catch { }
        }

        private void XoaMatHang()
        {
            try
            {
                var result = MessageBox.Show("Bạn Có Chắc Chắn Xóa Không?", "Thông Báo", MessageBoxButton.YesNo, MessageBoxImage.Question);
                if (result == MessageBoxResult.Yes)
                {
                    mathangContext.Xoa(int.Parse(txtMaMatHang.Text));
                    CapNhatDuLieuBang();
                    TrangThaiMacDinh();   
                    MessageBox.Show("Xóa Thành Công");   
                }
            }
            catch { MessageBox.Show("Chưa Thể Xóa"); }
        }

        private void LuuMatHang()
        {
            try
            {
                if (KiemTraNhap())
                {
                    if (Convert.ToInt32(txtMaMatHang.Text) < mathangContext.LayMaSoMoi())
                    {
                        var result = MessageBox.Show("Bạn Có Chắc Chắn Sửa Không?", "Thông Báo", MessageBoxButton.YesNo, MessageBoxImage.Question);
                        if (result == MessageBoxResult.Yes)
                        {
                            mathang = new CMatHang(int.Parse(txtMaMatHang.Text),txtTenMatHang.Text,Convert.ToInt32(txtSoLuongTon.Text),int.Parse(txtDonGiaQuyDinh.Text));
                            mathangContext.CapNhat(mathang);
                            MessageBox.Show("Sửa Thành Công");
                        }
                        TrangThaiCapNhat();

                    }
                    else
                    {
                        mathang = new CMatHang(int.Parse(txtMaMatHang.Text),txtTenMatHang.Text,Convert.ToInt32(txtSoLuongTon.Text), int.Parse(txtDonGiaQuyDinh.Text));
                        mathangContext.Them(mathang);
                        MessageBox.Show("Lưu Thành Công");
                        TrangThaiLuu();
                    }

                    CapNhatDuLieuBang();
                }
                

            }
            catch { MessageBox.Show("Chưa Lưu Được\nVui Lòng Kiểm Tra Lại Thông Tin Nhập"); }
            
            
        }

        private void ThemMatHang()
        {
            try
            {
                TrangThaiThem();
                txtTenMatHang.Clear();
                txtMaMatHang.Text = mathangContext.LayMaSoMoi().ToString();
                txtSoLuongTon.Clear();
            }
            catch { }
        }

        private void TrangThaiMacDinh()
        {
            btXoa.IsEnabled = false;
            btLuu.IsEnabled = false;
            txtSoLuongTon.IsEnabled = false;
            txtMaMatHang.IsEnabled = false;
            txtTenMatHang.IsEnabled = false;
            txtDonGiaQuyDinh.IsEnabled = false;
                
            txtTenMatHang.Clear();
            txtSoLuongTon.Clear();
            txtMaMatHang.Clear();
            txtDonGiaQuyDinh.Clear();
          
        }

        private void TrangThaiThem()
        {
            txtTenMatHang.IsEnabled = true;
            txtSoLuongTon.IsEnabled = true;
            txtDonGiaQuyDinh.IsEnabled = true;
            btLuu.IsEnabled = true;
            btXoa.IsEnabled = false;
            txtTenMatHang.Focus();
        }

        private void TrangThaiLuu()
        {
            txtTenMatHang.Clear();
            txtSoLuongTon.Clear();
            txtDonGiaQuyDinh.Clear();
            txtMaMatHang.Text = mathangContext.LayMaSoMoi().ToString();
            txtTenMatHang.Focus();
        }

        private void TrangThaiCapNhat()
        {
            txtSoLuongTon.IsEnabled = true;
            txtTenMatHang.IsEnabled = true;
            txtDonGiaQuyDinh.IsEnabled = true;
            btLuu.IsEnabled = true;
            btXoa.IsEnabled = true;
        }

        private bool KiemTraNhap()
        {
            if(txtSoLuongTon.Text.Length > 9 )
            {
                MessageBox.Show("Số Lượng Tồn Quá Lớn");
                txtSoLuongTon.Focus();
                txtSoLuongTon.Clear();
                return false;
            }
            if (txtTenMatHang.Text == "" || txtSoLuongTon.Text == "" || txtDonGiaQuyDinh.Text=="")
            {
                MessageBox.Show("Vui Lòng Nhập Đầy Đủ Thông Tin");
                return false;
            }
            return true;
        }

        #endregion
       
        #region EVEN





        private void btPhieuXuatMoi_Click(object sender, RoutedEventArgs e)
        {
            ThemMatHang();
        }

        private void btLuu_Click(object sender, RoutedEventArgs e)
        {
            LuuMatHang();
        }

        private void btXoa_Click(object sender, RoutedEventArgs e)
        {
            XoaMatHang();
        }

        private void txtSoLuongTon_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                LuuMatHang();
            }
        }

        private void lvMatHang_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ChonMatHang();
        }

        private void txtSoLuongTon_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (!Char.IsDigit((char)KeyInterop.VirtualKeyFromKey(e.Key)) &&
               !Char.IsControl((char)KeyInterop.VirtualKeyFromKey(e.Key)))
            {
                e.Handled = true;
            }
        }

        #endregion

    }
        
}
